<template>
	<view class="goods">
		<image :src="item.imgUrl" class="goodsImg"></image>
		<view class="center">
			<view class="goodsName">{{item.goodsName}}优惠卷</view>
			<view class="price">减免￥{{item.price}}</view>
		</view>
		<view class="right" v-if="isShopShow">
			<view class="points">消耗{{item.points}}积分</view>
			<button class="buyBtn" @click="exchangeHandle(item)">兑换</button>
		</view>
		<view class="right" v-else>
			<view class="number">×{{item.number}}</view>
			<view class="useBtn" @click="use(item.goodsId)">立即使用</view>
		</view>
	</view>
</template>

<script>
	export default {
		props: ['item', 'isShopShow'],
		data() {
			return {

			}
		},
		methods:{
			use(goodsId) {
				uni.navigateTo({
					url: `/pages/goodsDetail/goodsDetail?id=${goodsId}`
				})
			},
			//兑换
			exchangeHandle(item) {
				if (item.points > this.points) {
					uni.showToast({
						title: '积分不足',
						icon: 'none'
					})
					return;
				}
				uni.showModal({
					title: '确定要兑换吗',
					cancelText: '取消',
					confirmText: '确定',
					showCancel: true,
					success: (res) => {
						if (res.confirm) {
							this.exchange(item.id)
						}
					}
				})
			},
			async exchange(id){
				const res = await this.$myRequest({
					url: `shop/applet/buyDiscount/${id}`,
					method:'post'
				})
				
				if(res && res.code === 0){
					uni.showToast({
						title:'兑换成功',
						icon:'none'
					})
					this.$emit('getUserPoints')
				}
			}
		}
	}
</script>

<style>
</style>
